package com.qingxin.park.service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.qingxin.park.entity.ProRecord;
import com.qingxin.park.entity.ProRecordMaterial;
import com.qingxin.park.entity.ProRecordView;
import com.qingxin.repertory.entity.Repertory;
import com.qingxin.repertory.entity.RepertoryAccount;
import com.qingxin.repertory.service.RepertoryAccountService;
import com.qingxin.repertory.service.RepertoryService;
import com.qingxin.sys.entity.LoginUser;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.sys.service.BaseService;
import com.qingxin.sys.utils.StringUtils;


/**
 * 生产环节使用农资
 * @author Administrator
 *
 */
@Service
public class ProRecordMaterialService extends BaseService<ProRecordMaterial, String> {

	@Autowired
	private RepertoryService repertoryService;
	@Autowired
	private RepertoryAccountService repertoryAccountService;
	@Autowired
	private ProRecordService proRecordService;
	
	
	/**
	 * 获取使用农资
	 * @param recordid
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<ProRecordMaterial> listRecordMaterial(String recordid){
		List<ProRecordMaterial> list = listByHql("from ProRecordMaterial p where p.proRecord.recordid=?", recordid);
		if(list == null){
			return new ArrayList<>();
		}
		return list;
	}
	
	
	public ProRecordMaterial getRecordMaterial(String recordid){
		return (ProRecordMaterial) getByHql("from ProRecordMaterial p where p.proRecord.recordid=?", recordid);
	}
	
	
	
	/**
	 * 添加使用农资
	 * @param recordid
	 * @param repertoryid
	 * @param loginUser
	 * @return
	 */
	public SysError add(String recordid, String repertoryid, Double materialAmount,
			LoginUser loginUser, HttpServletRequest request) throws Exception{
		
		ProRecord proRecord = new ProRecord();
		proRecord.setRecordid(recordid);
		
		if(StringUtils.isEmpty(repertoryid)){
			this.deleteByHql("delete from ProRecordMaterial p where p.proRecord.recordid=?", recordid);
		}else{
			ProRecordMaterial recordMaterial = (ProRecordMaterial) getByHql("from ProRecordMaterial p where p.proRecord.recordid=?", recordid);
			if(recordMaterial != null && repertoryid.equalsIgnoreCase(recordMaterial.getMaterialid())){
				return null;
			}
			this.deleteByHql("delete from ProRecordMaterial p where p.proRecord.recordid=?", recordid);
			
			Repertory repertory = repertoryService.get(repertoryid);
			if(repertory == null){
				return null;
			}
			
			if(materialAmount==null || materialAmount<0){
				throw new Exception("农资使用数量输入错误，必须为数字");
			}
			
			ProRecordView proRecordView = proRecordService.getProRecordView(recordid);
			String accountRemark = proRecordView.getParkName()+"-"+proRecordView.getBlockName()+"-"+proRecordView.getCropName()+"于"+new SimpleDateFormat("yyyy-MM-dd").format(new Date())+"使用";
			
			RepertoryAccount account = new RepertoryAccount(null, repertoryid, new BigDecimal(materialAmount),
					new Date(), "1", accountRemark, loginUser.getUsername(), loginUser.getLogin_name(), new Date());
			
			SysError err = repertoryAccountService.add(account, repertoryid, loginUser, request);
			
			recordMaterial = new ProRecordMaterial(proRecord, repertoryid, repertory.getMaterialName(),account.getAccountid(),
					materialAmount, repertory.getUnit(), "", loginUser.getLogin_name(), new Date());
			this.save(recordMaterial);
			
			
			if(err != null){
				throw new Exception("农资使用数量大于库存量，添加失败");
			}
			
		}
		return null;
	}
	
	
}
